package com.kakaxi.mybilibili.module.video.view;

import android.content.pm.ActivityInfo;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.pager.VideoPagerAdapter;
import com.kakaxi.mybilibili.base.NavigationActivity;
import com.kakaxi.mybilibili.bean.EventBusObject;
import com.kakaxi.mybilibili.bean.VideoBean;
import com.kakaxi.mybilibili.bean.VideoCommentBean;
import com.kakaxi.mybilibili.bean.VideoIntroBean;
import com.kakaxi.mybilibili.module.video.presenter.IVideoPresenter;
import com.kakaxi.mybilibili.module.video.presenter.VideoPresenter;
import com.kakaxi.mybilibili.utils.LogUtil;
import com.kakaxi.mybilibili.utils.ProgressDialogUtil;
import com.kakaxi.mybilibili.view.MyJCVideoPlayer;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

/**
 * 视频活动类
 * Created by kakaxi on 16/11/20.
 */

public class VideoActivity extends NavigationActivity implements IVideoView {

    /** 视频封面 **/
    @BindView(R.id.img_video_cover)
    ImageView mCoverImg;

    /** 标签布局 **/
    @BindView(R.id.tablayout_video_tab)
    TabLayout mTabLayout;

    /** 滑动布局 **/
    @BindView(R.id.vp_video_pager)
    ViewPager mViewPager;

    /** 伸缩工具栏 **/
    @BindView(R.id.coltoolbar_video_bar)
    CollapsingToolbarLayout mCollapsingToolbarLayout;

    /** 播放器 **/
    @BindView(R.id.jcvplayer_video_player)
    MyJCVideoPlayer mPlayer;

    /** 播放器正文 **/
    @BindView(R.id.flayout_video_player_content)
    FrameLayout mPlayerContent;

    /** 播放按钮 **/
    @BindView(R.id.floatBtn_video_play)
    FloatingActionButton mPlayFloatBtn;

    /** 播放器工具栏 **/
    @BindView(R.id.toolbar_video)
    Toolbar mVideoToolbar;

    /** 应用栏 **/
    @BindView(R.id.appbar)
    AppBarLayout mAppBar;

    /** 视频封面传递参数 **/
    public static final String EXTRA_VIDEO_COVER_URL = "coverUrl";
    /** 视频a编号传递参数 **/
    public static final String EXTRA_VIDEO_AID = "aid";

    /** 控制层 **/
    private IVideoPresenter mPresenter;

    /** 视频信息 **/
    private VideoBean mVideoBean;
    /** 视频介绍 **/
    private VideoIntroBean mVideoIntroBean;
    /** 视频播放链接 **/
    private List<String> mPlayUrlList;

    /** 视频的a编号 **/
    private String mAid;

    @Override
    protected int getCurrentMenuItemId() {
        return R.menu.menu_nav;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_video;
    }

    @Override
    protected void setupViews() {
        ButterKnife.bind(this);
        showBackBtn();

        // 初始化滑动布局
        mAid = getIntent().getStringExtra(EXTRA_VIDEO_AID);
        mViewPager.setAdapter(new VideoPagerAdapter(getSupportFragmentManager(), mAid));
        // 初始化标签布局
        mTabLayout.setupWithViewPager(mViewPager);
        // 初始化播放按钮
        mPlayFloatBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPlayUrlList != null) {
                    // 展示播放器
                    mPlayerContent.setVisibility(View.VISIBLE);
                    // 隐藏取消伸缩效果
                    ((AppBarLayout.LayoutParams) mCollapsingToolbarLayout.getLayoutParams())
                            .setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP);
                    mCollapsingToolbarLayout.setVisibility(View.GONE);
                    mPlayFloatBtn.setVisibility(View.GONE);
                    // 把播放器的工具栏设置为actionbar
                    setSupportActionBar(mVideoToolbar);
                    showBackBtn();
                    // 播放
                    mPlayer.startPlayLogic();
                } else {
                    toastHandler("请稍等,视频初始化中..");
                }
            }
        });
        // 初始化播放器
        mPlayer.tinyBackImageView.setVisibility(View.INVISIBLE);
        mPlayer.setOnVisibilityChangedListener(new MyJCVideoPlayer.OnVisibilityChangedListener() {
            @Override
            public void onVisibilityChanged(int currentScreenState, int currentPlayerState, int
                    currentViewVisibilityState) {
                if (currentScreenState != JCVideoPlayer.SCREEN_WINDOW_FULLSCREEN) {
                    mVideoToolbar.setVisibility(currentViewVisibilityState);
                }
            }
        });
        // 初始化应用栏
        mAppBar.getContext().setTheme(R.style.toolbar_white_theme);
        mAppBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                // 当缩小完成时隐藏工具栏的背景, 拉开时候显示背景
                if (verticalOffset < mCollapsingToolbarLayout.getScrimVisibleHeightTrigger() -
                        appBarLayout.getHeight()) {
                    getToolBar().setBackgroundResource(android.R.color.transparent);
                } else {
                    getToolBar().setBackgroundResource(R.drawable.bg_common_black_gradient);
                }
            }
        });

    }

    @Override
    protected void initData() {
        mPresenter = new VideoPresenter(this);
        mPresenter.initModel(this);
        // 封面
        String coverUrl = getIntent().getStringExtra(EXTRA_VIDEO_COVER_URL);
        if (coverUrl != null) {
            Glide.with(getBaseContext()).load(coverUrl).crossFade().into(mCoverImg);
        }

        // 视频的a编号
        if (mAid != null) {
            setTitle("av" + mAid);
            mPresenter.initVideoData(mAid);
        } else {
            toastHandler("错误:缺少参数");
        }
    }


    @Override
    protected void onStop() {
        super.onStop();
        // 停止播放
        mPlayer.release();
    }

    @Override
    public void showVideoData(List<String> videoUrlList) {
        if (videoUrlList != null && videoUrlList.size() > 0) {
            LogUtil.d("播放链接:" + videoUrlList);
            mPlayUrlList = videoUrlList;
            mPlayer.setUp(videoUrlList.get(0), JCVideoPlayer.SCREEN_LAYOUT_LIST, getTitle());
            // 把小屏播放器的标题去掉
            mPlayer.titleTextView.setText("");
        }

    }

    @Override
    public void showVideoIntroData(VideoIntroBean videoIntroBean) {
        if (videoIntroBean != null) {
            mVideoIntroBean = videoIntroBean;
            EventBusObject object = new EventBusObject(EventBusObject
                    .EVENT_VIDEO_INTRO_FRAG_SHOW_INFO, videoIntroBean);
            EventBus.getDefault().post(object);
        }
    }

    @Override
    public void showVideoCommentData(VideoCommentBean videoCommentBean) {

    }

    @Override
    public void showLoading() {
        ProgressDialogUtil.show(this);
    }

    @Override
    public void hideLoading() {
        ProgressDialogUtil.hide();
    }

    @Override
    public void showEmptyView() {

    }

    @Override
    public void showMessage(String message) {
        toastHandler(message);
    }

    @Override
    @Deprecated
    public void showVideoData(VideoIntroBean videoIntroBean, VideoBean videoBean) {
        if (videoIntroBean != null) {
            mVideoIntroBean = videoIntroBean;
            EventBus.getDefault().post(mVideoIntroBean);
        }
        if (videoBean != null) {
            mVideoBean = videoBean;
            String url = videoBean.getDurl().get(0).getUrl();
            LogUtil.d(url);
            mPlayer.setUp(url, JCVideoPlayer.SCREEN_LAYOUT_NORMAL, "");
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && getRequestedOrientation() == ActivityInfo
                .SCREEN_ORIENTATION_LANDSCAPE) {
            JCVideoPlayer.backPress();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
